package com.gzhryc.net_marketing.supplier.erp.services;

import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.net_marketing.supplier.erp.dao.SupplierGoodsSpaceDao;
import com.gzhryc.net_marketing.supplier.erp.dao.db.SupplierGoodsSpace;
import com.gzhryc.net_marketing.supplier.erp.dao.enums.SupplierGoodsEnum;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class SupplierGoodsSpaceService {

    static Logger log = Logger.getLogger(SupplierGoodsSpaceService.class);

    public static SupplierGoodsSpaceService self() {
        return MultiDBTools.getService(SupplierGoodsSpaceService.class, true);
    }

    public static SupplierGoodsSpaceService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, SupplierGoodsSpaceService.class, true);
    }

    SupplierGoodsSpaceDao dao;

    public SupplierGoodsSpaceService(String jdbcKey) {
        dao = new SupplierGoodsSpaceDao(jdbcKey);
    }

    public List<SupplierGoodsSpace> findByGoodsId(Long goodsId){
        if(goodsId != null){
            Conditions conditions = new Conditions(SupplierGoodsSpace.class);
            conditions.and().is("goodsId",goodsId);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public Long countByGoodsId(Long goodsId){
        if(goodsId != null){
            Conditions conditions = new Conditions(SupplierGoodsSpace.class);
            conditions.and().is("goodsId",goodsId);
            try {
                return dao.count(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return 0L;
    }

    /**
     * 获取上架的商品
     * @param goodsId
     * @return
     */
    public List<SupplierGoodsSpace> findPutByGoodsId(Long goodsId){
        if(goodsId != null){
            Conditions conditions = new Conditions(SupplierGoodsSpace.class);
            conditions.and().is("goodsId",goodsId);
            conditions.and().is("state", SupplierGoodsEnum.State.PutOnTheShelf.index());
            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public SupplierGoodsSpace getByKeys(Long goodsId,Long id){
        if(goodsId != null && id != null){
            Conditions conditions = new Conditions(SupplierGoodsSpace.class);
            conditions.and().is("goodsId",goodsId);
            conditions.and().is("id",id);

            try {
                return dao.get(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public Long maxIdByGoodsId(Long goodsId){
        if(goodsId != null){
            try {
                Long id =  dao.maxIdByGoodsId(goodsId);
                if(id != null){
                    return id;
                }
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return 0L;
    }

    public boolean add(SupplierGoodsSpace GoodsSpace) throws LogicException {
        Long id = maxIdByGoodsId(GoodsSpace.getGoodsId()) + 1;
        SupplierGoodsSpace temp = getByKeys(GoodsSpace.getGoodsId(), id);
        if(temp != null){
            throw new LogicException("同步添加，请稍后再试");
        }
        GoodsSpace.setId(id);
        GoodsSpace.setCreateDate(new Date());
        try {
            return dao.insert(GoodsSpace) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean updateNotNull(SupplierGoodsSpace GoodsSpace)throws LogicException {
        if(GoodsSpace.getGoodsId() != null && GoodsSpace.getId() != null){
            Conditions conditions = new Conditions(SupplierGoodsSpace.class);
            conditions.and().is("goodsId",GoodsSpace.getGoodsId());
            conditions.and().is("id",GoodsSpace.getId());

            try {
                return dao.updateNotNull(GoodsSpace,conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }

    public boolean deleteByKeys(Long goodsId,Long id)throws LogicException {
        if(goodsId != null && id != null){
            Conditions conditions = new Conditions(SupplierGoodsSpace.class);
            conditions.and().is("goodsId",goodsId);
            conditions.and().is("id",id);

            try {
                return dao.deleteByCondition(conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }

    public boolean deleteByGoodsId(Long goodsId){
        if(goodsId != null){
            Conditions conditions = new Conditions(SupplierGoodsSpace.class);
            conditions.and().is("goodsId",goodsId);

            try {
                return dao.deleteByCondition(conditions) >= 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }

}
